// src/file/file.module.ts
import { Module } from '@nestjs/common';
import { MulterModule } from '@nestjs/platform-express';
import { FileController } from './file.controller';
import { FileService } from './file.service';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule, // 明确导入 ConfigModule
    // 动态配置上传目录和文件限制
    MulterModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (config: ConfigService) => ({
        storage: diskStorage({
          destination: config.get('UPLOAD_DIR', './uploads'), // 默认上传到 ./uploads
          filename: (req, file, callback) => {
            const randomName = Array(32)
              .fill(null)
              .map(() => Math.round(Math.random() * 16).toString(16))
              .join('');
            callback(null, `${randomName}${extname(file.originalname)}`);
          },
        }),
        limits: {
          fileSize: config.get('MAX_FILE_SIZE', 10 * 1024 * 1024), // 默认10MB
        },
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [FileController],
  providers: [FileService],
  exports: [FileService], // 导出服务供其他模块使用
})
export class FileModule { }